import {groupBy} from 'lodash'

const dict = {
  state: {
    dataScopeList: [],
    dataScopeMap: {},
    productCategoryList: [],
    productCategoryMap: {},
    protocolList: [],
    protocolMap: {},
    propDataTypeList: [],
    propDataTypeMap: {},
    propDataMethodList: [],
    propDataMethodMap: {},
    deviceTypeList: [],
    deviceTypeMap: {},
    deviceTypeColorMap: {},
    pushTargetList: [],
    pushTargetMap: {},
    pushTargetColorMap: {},
    actionAlarmTypeList: [],
    actionAlarmTypeMap: {},
    actionAlarmLevelList: [],
    actionAlarmLevelMap: {},
    actionAlarmLevelColorMap: {},
  },
  mutations: {
    SET_DICTS: (state, dicts) => {
      const map = groupBy(dicts, 'type')
      // dataScope
      state.dataScopeList = map['data_scope']
      state.dataScopeList.forEach(obj => {
        state.dataScopeMap[parseInt(obj.code)] = obj.name
      })
      // productCategory
      state.productCategoryList = map['product_category']
      state.productCategoryList.forEach(obj => {
        state.productCategoryMap[parseInt(obj.code)] = obj.name
      })
      // protocol
      state.protocolList = map['protocol']
      state.protocolList.forEach(obj => {
        state.protocolMap[obj.code] = obj.name
      })
      // prop_data_type
      state.propDataTypeList = map['prop_data_type']
      state.propDataTypeList.forEach(obj => {
        state.propDataTypeMap[obj.code] = obj.name
      })
      // prop_data_method
      state.propDataMethodList = map['prop_data_method']
      state.propDataMethodList.forEach(obj => {
        state.propDataMethodMap[obj.code] = obj.name
      })
      // device_type
      state.deviceTypeList = map['device_type']
      state.deviceTypeList.forEach(obj => {
        const code = parseInt(obj.code)
        state.deviceTypeMap[code] = obj.name
        switch (code) {
          case 1:
            state.deviceTypeColorMap[code] = 'blue'
            break;
          case 2:
            state.deviceTypeColorMap[code] = 'green'
            break;
          case 3:
            state.deviceTypeColorMap[code] = 'pink'
            break;
          case 4:
            state.deviceTypeColorMap[code] = 'orange'
            break;
          default:
            state.deviceTypeColorMap[code] = 'purple'
        }
      })
      // push_target
      state.pushTargetList = map['push_target']
      state.pushTargetList.forEach(obj => {
        const code = parseInt(obj.code)
        state.pushTargetMap[code] = obj.name
        switch (code) {
          case 1:
            state.pushTargetColorMap[code] = 'blue'
            break;
          case 2:
            state.pushTargetColorMap[code] = 'green'
            break;
          case 3:
            state.pushTargetColorMap[code] = 'pink'
            break;
          case 4:
            state.pushTargetColorMap[code] = 'orange'
            break;
          default:
            state.pushTargetColorMap[code] = 'purple'
        }
      })
      // action_alarm_type
      state.actionAlarmTypeList = map['action_alarm_type']
      state.actionAlarmTypeList.forEach(obj => {
        state.actionAlarmTypeMap[parseInt(obj.code)] = obj.name
      })
      // action_alarm_level
      state.actionAlarmLevelList = map['action_alarm_level']
      state.actionAlarmLevelList.forEach(obj => {
        const code = parseInt(obj.code)
        state.actionAlarmLevelMap[code] = obj.name
        switch (code) {
          case 1:
            state.actionAlarmLevelColorMap[code] = 'orange'
            break;
          case 2:
            state.actionAlarmLevelColorMap[code] = 'pink'
            break;
          case 3:
            state.actionAlarmLevelColorMap[code] = 'red'
            break;
          case 4:
            state.actionAlarmLevelColorMap[code] = 'purple'
            break;
          default:
            state.actionAlarmLevelColorMap[code] = 'purple'
        }
      })

    }
  },
  actions: {}
}

export default dict
